package com.baizhi.controller;

import com.baizhi.AdminLogAnnoation.AdminLogAnnotion;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.service.CmfzAdminService;
import com.baizhi.service.CmfzSuperAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: renpu
 * @Date: 2019/2/26 20:50
 * @Description:
 */
@Controller
@RequestMapping("/Admin")
public class CmfzAdminController {
    @Autowired
    private CmfzAdminService adminService;

    @Autowired
    private CmfzSuperAdminService cmfzSuperAdminService;



    @RequestMapping("/showAdmin")
    @ResponseBody
    public Map showAdmin(int rows,int page,Map map){
        return adminService.selectAdmin(rows,page);
    }



    @RequestMapping("/login")
    @AdminLogAnnotion("登录成功")
    public String login(String userName,String passWord, Map map){
        map=cmfzSuperAdminService.selectNameAndPassword(userName,passWord);
        boolean meg = (boolean)map.get("mesg");
        if(meg){
            Subject subject = SecurityUtils.getSubject();
            String use= (String) subject.getPrincipal();

            //编码式授权；
            if(subject.hasRole("admin")){
                System.out.println("此用户拥有普通管理的身份");
            }
            return "main";
        }else {
            return "redirect:/login.jsp";
        }
    }


    @RequestMapping("/exit")
    public String clearSession(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.jsp";
    }
}
